package together.news.common.db.file;

import java.io.File;
import java.io.IOException;


public class FileHanderHelper {
	private static int maxFileCacheForAURL=10;
	public  File maintainDownloadFileSystem(String group,String sourceType, String sourceURL) throws IOException {
		File siteFile = new File(FileSystemConfig.getDownloadFolderStructure(sourceURL,group,sourceType));
		if (siteFile.exists() == false) {
			siteFile.mkdirs();
		}
		File[] dataFiles=siteFile.listFiles();
		for(int i=0;dataFiles!=null && i<dataFiles.length-maxFileCacheForAURL;i++){
			dataFiles[i].delete();
		}
		String currentFileName=FileSystemConfig.getDownloadFileName(sourceType,sourceURL);
		File saveFile = new File(siteFile.getAbsolutePath()+"/"+currentFileName);
		if (saveFile.exists() == false) {
			saveFile.createNewFile();
		}
		return saveFile;
	}
	public File maintainGeneratedFileSystem(String pageName) throws IOException {
		File siteFile = new File(FileSystemConfig.generate_FOLDER);
		if (siteFile.exists() == false) {
			siteFile.mkdirs();
		}
		String currentFileName=pageName;
		File saveFile = new File(FileSystemConfig.generate_FOLDER+"/"+currentFileName);
		if (saveFile.exists() == false) {
			saveFile.createNewFile();
		}
		return saveFile;
	}

}
